// pages/my/my.js
const app = getApp();
Page({
  data: {
    // 初始化数据
    isVip: false,
    vip: {}
  },
  onLoad() {
    // 页面加载时的初始化操作
  },
  goSetting() {
    wx.navigateTo({
      url: './setting/setting',
    })
  },
  onShow() {
    if (!app.globalData.token) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      setTimeout(()=> {
        wx.switchTab({
          url: '/pages/index/index',
        });
      }, 1500);
      return;
    }
    const data = {};
    
    let str_rex = JSON.stringify(app.globalData.userinfo.vip);
    if (str_rex !== JSON.stringify(this.data.vip)) {
      console.log('this.data VIP', this.data.vip)
      console.log('app.globalData.userinfo.vip', app.globalData.userinfo.vip)
      let vip = app.globalData.userinfo.vip;
      if (str_rex !== '{}' && vip.valid_date.length > 10) {
        vip.valid_date = vip.valid_date.slice(0, -9);
      }
      data.vip = vip;
      data.isVip = str_rex !== '{}';
    }
    
    if (app.globalData.userinfo.avatar !== this.avatar) {
      this.avatar = app.globalData.userinfo.avatar;
      let presuffix = '';
      if (this.avatar.charAt(0) === '/') {
        presuffix = app.globalData.requestUrl;
      }
      data.avatar = presuffix + this.avatar;
    }
    if (app.globalData.userinfo.nickname !== this.data.nickname) {
      data.nickname = app.globalData.userinfo.nickname;
    }
    if (app.globalData.userinfo.group_id !== this.group_id) {
      data.group_id = app.globalData.userinfo.group_id;
      if (data.group_id < 1) {
        data.group_id = 1;
      }
      if (data.group_id > 5) {
        data.group_id = 5;
      }
    }
    this.setData(data);
  },
  goBuy() {
    wx.navigateTo({
      url: '/pages/buy/buy'
    });
  },
  go1() {
    wx.navigateTo({
      url: './share-info/share-info',
    });
  },
  go2() {
    wx.navigateTo({
      url: './buy-list/buy-list',
    });
  },
  goDesc(e) {
    wx.navigateTo({
      url: './desc/desc?name=' + e.currentTarget.dataset.name,
    });
  },
  fun2() {
    wx.showActionSheet({
      itemList: app.globalData.phoneNumber,
      success (res) {
        wx.makePhoneCall({
          phoneNumber: app.globalData.phoneNumber[res.tapIndex]
        });
      },
      fail (res) {
        console.log(res.errMsg)
      }
    })
  }
})
  